# Script by Danke!
from twisted.internet.reactor import callLater
from pyspades.server import weapon_reload
from pyspades.collision import distance_3d_vector
from pyspades.constants import *
from commands import add
import random
import commands

ONESHOT, FRAILTY, BROMANCE, ALLERGY, WALLET, BUTTERFINGER, RUBBER =  xrange(7)

def handicap(connection, value = 7):
    value = int(value)
    if value > 6:
        connection.send_chat("5 - Butterfingers   6 - Rubber Bullets  ")
        connection.send_chat("3 - Lead Allergy    4 - Empty Wallet    ")
        connection.send_chat("1 - Frailty         2 - Tragic Bromance ")
        connection.send_chat("0 - One Shot        ")
        connection.send_chat("Pick a handicap with /handicap #:")
        return
    elif value == ONESHOT:
        connection.handicap = value
        connection.send_chat("0 - One Shot - Your gun only holds one bullet in the chamber")
        connection.send_chat("Handicap changed to:")
        connection.kill()
    elif value == FRAILTY:
        connection.handicap = value
        connection.send_chat("1 - Frailty - All hits against you instakill")
        connection.send_chat("Handicap changed to:")
        connection.kill()
    elif value == BROMANCE:
        connection.handicap = value
        connection.send_chat("2 - Tragic Bromance - Every time a teammate near you dies, you lose 1/4 your health")
        connection.send_chat("Handicap changed to:")
        connection.kill()
    elif value == ALLERGY:
        connection.handicap = value
        connection.send_chat("3 - Lead Allergy - All bullets poison you 1hp/s, and it stacks")
        connection.send_chat("Handicap changed to:")
        connection.kill()
    elif value == WALLET:
        connection.handicap = value
        connection.send_chat("4 - Empty Wallet - You only get one magazine")
        connection.send_chat("Handicap changed to:")
        connection.kill()
    elif value == BUTTERFINGER:
        connection.handicap = value
        connection.send_chat("5 - Butterfingers - Getting hit removes your weapon")
        connection.send_chat("Handicap changed to:")
        connection.kill()
    elif value == RUBBER:
        connection.handicap = value
        connection.send_chat("6 - Rubber Bullets - Your bullets do 1/2 damage")
        connection.send_chat("Handicap changed to:")
        connection.kill()
add(handicap)

def apply_script(protocol, connection, config):
    class HandicapConnection(connection):
        def _on_reload(self):
            if self.handicap == 0:
                callLater(0.1, self.handicap_one_shot)
            weapon_reload.player_id = self.player_id
            weapon_reload.clip_ammo = self.weapon_object.current_ammo
            weapon_reload.reserve_ammo = self.weapon_object.current_stock
            self.send_contained(weapon_reload)

        def handicap_one_shot(player):
            weapon = player.weapon_object
#            weapon.set_shoot(False)
            weapon.current_ammo = 1
            weapon_reload.player_id = player.player_id
            weapon_reload.clip_ammo = 1
            weapon_reload.reserve_ammo = 50
            player.send_contained(weapon_reload)

        def handicap_wallet_ammo(player):
            weapon = player.weapon_object
            weapon.set_shoot(False)
            weapon.current_ammo = player.weapon_object.ammo
            weapon_reload.player_id = player.player_id
            weapon_reload.clip_ammo = player.weapon_object.ammo
            weapon_reload.reserve_ammo = 0
            player.send_contained(weapon_reload)

        def handicap_drop_weapon(player):
            weapon = player.weapon_object
            weapon.set_shoot(False)
            weapon.current_ammo = 0
            weapon_reload.player_id = player.player_id
            weapon_reload.clip_ammo = 0
            weapon_reload.reserve_ammo = 0
            player.send_contained(weapon_reload)

        def on_login(self, name):
            self.handicap = random.randint(0,6)
            connection.on_login(self, name)

        def explain_handicap(self):
            message = ("One Shot") if self.handicap == 0 else ""
            message += ("Frailty") if self.handicap == 1 else ""
            message += ("Tragic Bromance") if self.handicap == 2 else ""
            message += ("Lead Allergy") if self.handicap == 3 else ""
            message += ("Empty Wallet") if self.handicap == 4 else ""
            message += ("Butterfinger") if self.handicap == 5 else ""
            message += ("Rubber Bullets") if self.handicap == 6 else ""
            return(message)
            
        def on_hit(self, hit_amount, hit_player, type, grenade):
            value = connection.on_hit(self, hit_amount, hit_player, type, grenade)
            if value is not None or hit_player.team == self.team:
                return value
            value = hit_amount
            if self.handicap == 6:
                value *= .50
            if hit_player.handicap == 1:
                value = 100
            if hit_player.handicap == 3:
                hit_player.send_chat("You have been poisoned by %s ! Get to the tent to cure it!" % self.name)
                hit_player.poisoner = self
                hit_player.poison += 1
            if hit_player.handicap == 5:
                hit_player.handicap_drop_weapon()
            return value

        def on_kill(self, killer, type, grenade):
            if killer != self and killer is not None:
                self.send_chat("Type /handicap for more info on Handicaps")
                self.send_chat("You were killed by %s who had %s" % (killer.name, killer.explain_handicap()))
                self.handicap_check_team()
            return connection.on_kill(self, killer, type, grenade)

        def on_spawn(self, pos):
            if self.handicap == 0:
                self.handicap_one_shot()
            if self.handicap == 4:
                self.handicap_wallet_ammo()
            self.poisoner = None
            self.poison = 0
            self.send_chat("Type /handicap for more information!")
            self.send_chat("You have %s" % self.explain_handicap())
            return connection.on_spawn(self, pos)
        
        def handicap_check_team(self):
            for player in self.team.get_players():
                if player is not None and player.hp is not None and value is not None and player.hp > 0:
                    dist = distance_3d_vector(self.world_object.position, player.world_object.position)
                    if dist <= 32.0 or player == self:
                        if player != self and player.handicap == 2 and player.hp > 1:
                            player.send_chat("%s died. This makes you sad" % self.name)
                            player.hit(int(player.hp/4), player)

        def handicap_every_second(self):
            if self is None or self.hp <= 0:
                return
            if self.poison > 0 and self.poisoner is not None and self.poisoner.world_object is not None:
                self.hit(self.poison, self.poisoner)

        def on_refill(self):
            if self.handicap == 0:
                self.handicap_one_shot()
            if self.poison > 0:
                self.send_chat("You have been cured of the poison!")
                self.poison = 0
                self.poisoner = None
            if self.handicap == 4:
                callLater(0.1, self.handicap_wallet_ammo)
            return connection.on_refill(self)

        def handicap_clear(self):
            self.poisoner = None
            self.poison = 0

            
    class HandicapProtocol(protocol):      
        handicap_second_counter = 0

        def on_world_update(self):
            self.handicap_second_counter += 1
            if self.handicap_second_counter >= 90:
                for player in self.players.values():
                    player.handicap_every_second()
                self.handicap_second_counter = 0
            protocol.on_world_update(self)

    return HandicapProtocol, HandicapConnection
